package com.lifesea.notify.mq.handler;

import com.alibaba.fastjson.JSON;
import com.alibaba.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import com.alibaba.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import com.alibaba.rocketmq.common.message.MessageExt;
import com.lifesea.commons.utils.HttpUtils;
import com.lifesea.conifg.PublicConfig;
import com.lifesea.model.dto.BookOrderNotifyRequest;
import com.lifesea.rocketmq.utils.handler.MessageHandler;
import com.lifesea.rocketmq.utils.message.MessageDecoder;
import com.lifesea.rocketmq.utils.message.codec.JsonMessageDecoder;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 挂号消息处理器
 */
@Component
public class BookOrderNotifyAppHandler implements MessageHandler<BookOrderNotifyRequest> {

    private static final Logger logger = LoggerFactory.getLogger(BookOrderNotifyAppHandler.class);

    private final String URL = PublicConfig.BOOK_NOTIFY_URL;

    @Override
    public ConsumeConcurrentlyStatus process(BookOrderNotifyRequest data, MessageExt message, ConsumeConcurrentlyContext context) {
        logger.info("[挂号通知居民端]接收,msgId={},notify={}", message.getMsgId(), data);

        try {

            Map<String, String> params = new HashMap<>();
            params.put("creater_ecn", String.valueOf(data.getOrder().getEcn()));
            params.put("hospital", data.getOrder().getHosName());
            params.put("bespeak_name", data.getOrder().getPatName());
            params.put("bespeak_mobile", data.getPatient().getPatMobile());
            params.put("bespeak_idcard", data.getPatient().getIdcard());
            params.put("doct_name", data.getOrder().getDocName());
            params.put("doct_dept", data.getOrder().getDeptName());
            params.put("book_time", DateFormatUtils.format(data.getOrder().getSchDate(), "yyyy-MM-dd HH:mm:ss"));
            String response = HttpUtils.doPost(URL, params);
            logger.info("推送居民APP消息结果,req={},res={}", JSON.toJSONString(params), response);

            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        } catch (Exception e) {
            logger.error("[挂号通知居民端]处理失败", e);
        }
        return ConsumeConcurrentlyStatus.RECONSUME_LATER;

    }


    @Override
    public MessageDecoder<BookOrderNotifyRequest> getMessageDecoder() {
        return new JsonMessageDecoder<>(BookOrderNotifyRequest.class);
    }
}
